<?php
function_exists('UnixPath')  || die('OK');


	$ACT = array('list', 'add', 'aok', 'edit', 'eok', 'del');
	in_array($_GET['act'], $ACT) || $_GET['act'] = 'list';

	//$html['tpl_right_act'] = $_GET['act'];

	$_GET['id']	= (int) $_GET['id'];
	$_GET['pid']	= (int) $_GET['pid'];

	$P = E::$DB->one('_super_param', 'id=' . $_GET['pid']);	/*	父栏目 */
	$P['id'] || $P = array('id'=>0, 'paramName'=>'根节点','deep'=>'0');

	$body['navlist'] = array(
		'list'	=> '参数节点列表' ,
		'add'	=> '添加参数节点' ,
	);

	$body['_url'] = '&pid='. $P[id];
	$body['url'] = $html['url'] .$body['_url'] .'&';


	$body['node_tree'] = node_tree($P); 

	
if( $_GET['act'] === 'aok' ){
	$_POST['pid'] = $P['id'];
	$_POST['deep'] = $P['deep'] + 1;

	E::$DB->insert('_super_param', 'pid, deep, isParent, paramKey,paramValue, paramName,orderId ,isReferer');
	
	C::table();
	C::param();
	A::msg('添加参数节点完成!', $_POST['jurl'], 1, 1 );
}

if( $_GET['act'] == 'edit' ){
	$L = E::$DB->one('_super_param', 'id=' . $_GET['id']);
	$L['id'] || A::msg('无效的节点', $_POST['jurl'], 1, 1 );
	$L['isCanChangeParent'] = count( E::$DB->all('_super_param','pid=' . $L['id']) );
}

if( $_GET['act'] === 'eok' ){
	$L = E::$DB->one('_super_param', 'id=' . $_GET['id']);

	if( empty($L['id']) ){
		A::msg('无效的节点', $_POST['jurl'], 1, 1 );
	}


	if( $L['id'] < 11 && $_POST['isParent'] != $L['isParent'] ){
		A::msg('你没有权限操作此节点!', $_POST['jurl'], 1, 1 );
	}


	$ups = ' paramValue, paramKey, paramName,orderId, isReferer';

	if( !count(E::$DB->all('_super_param', 'pid='. $L['id'] )) ){
		$ups .= ',isParent';
	}

	E::$DB->update('_super_param', $ups , 'id=' . $L['id']);
	
	C::table();
	C::param();
	A::msg('修改参数节点 ' . $_POST['paramName'] . ' 完成!', $_POST['jurl'], 1, 1 );
}


if( $_GET['act'] === 'del' ){

	$L = E::$DB->one('_super_param','id=' . $_GET['id']);

	if(  empty($L['id']) || $L['id'] < 11 ){
		A::msg('你没有权限操作此节点!', $_POST['jurl'], 1, 1 );
	}

	if( count(E::$DB->all('_super_param', 'pid=' .$L['id'])) ){
		A::msg('您必须先删除该节点的子节点!', '',1,1);
	}

	E::$DB->query('DELETE FROM _super_param WHERE id=' . $L['id'] . ' or pid=' .$L['id']);	
	C::table();
	C::param();
	A::msg('删除参数节点 ' . $L['paramName'] . ' 完成!', $_POST['jurl'], 1, 1 );
}


if( $_GET['act'] === 'list' ){
	$p = E::$DB->page('_super_param', 'pid=' .$P['id']);	
	$L = E::$DB->all('_super_param', 'pid=' .$P['id'] . ' order BY orderId DESC,id ASC ' . $p['limit']);
}

/***********************************/

function node_tree($p){
	global $html;
	if(!$p['id']) {
		return '<a href="' . $html['url'] . '&pid=0&act=list">根节点</a>';
	}
	$s = '<a href="' . $html['url'] . '&pid='. $p['id'] . '&act=list">' . $p['paramName'] . '</a>';
	if( $p['pid'] ) {
		$s = node_tree(E::$DB->one('_super_param', 'id=' . $p['pid'])) .' <small>&#187;</small> '. $s;
	}else{
		$s = '<a href="' . $html['url'] . '&pid=0&act=list">根节点</a> <small>&#187;</small> '. $s;
	}
	return $s;
}

